package com.vnp.vchat.version2;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.vnp.vchat.R;
import com.vnp.vchat.R.anim;
import com.vnp.vchat.R.id;
import com.vnp.vchat.R.layout;
import com.vnp.vchat.Util.KeyboardHelper;
import com.vnp.vchat.Util.Utils;
import com.vnp.vchat.httpclient.HttpClientHelper;
import com.vnp.vchat.httpclient.MyJsonHttpResponseHandler;

public class DangkiNhapPhoneActivity extends Activity implements OnClickListener{

	TextView title, btnFunction2;
	EditText edtPhone;
	
	Button btnTieptuc;	
	ImageButton btnBack;
	
	TextView  titleNhapSodt;
	
	Handler mHandler = new Handler();
	Runnable r = new Runnable() {
		
		@Override
		public void run() {
			KeyboardHelper.displaySoftKeyboard(DangkiNhapPhoneActivity.this, edtPhone);
		}
	};
	
	TextView txtTenQuocgia, txtMavung;
	
	Dialog dConfirm;
	
	String accID;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dangki_nhap_phone);
		
		title = (TextView) findViewById(R.id.title);
		btnFunction2 = (TextView) findViewById(R.id.btn_funtion2);
		btnFunction2.setVisibility(View.GONE);
		btnBack = (ImageButton) findViewById(R.id.btn_function1);
		btnBack.setVisibility(View.VISIBLE);
		
		edtPhone = (EditText) findViewById(R.id.edt_phone);
		
		btnTieptuc = (Button) findViewById(R.id.btn_tieptuc);
		
		titleNhapSodt = (TextView) findViewById(R.id.txt_nhap_sodt);
		txtTenQuocgia = (TextView) findViewById(R.id.txt_ten_quocgia);
		txtMavung = (TextView) findViewById(R.id.txt_ma_quocgia);
		
		mHandler.postDelayed(r, 300);
		
		setFont();
		
		init();
	}
	
	void init(){
		title.setText("Đăng ký");
		btnBack.setOnClickListener(this);
		btnTieptuc.setOnClickListener(this);
	}
	
	void setFont(){
		title.setTypeface(Utils.getTypefaceBold(DangkiNhapPhoneActivity.this));
		edtPhone.setTypeface(Utils.getTypefaceRegular(DangkiNhapPhoneActivity.this));
		btnTieptuc.setTypeface(Utils.getTypefaceMedium(DangkiNhapPhoneActivity.this));
		txtTenQuocgia.setTypeface(Utils.getTypefaceRegular(DangkiNhapPhoneActivity.this));
		txtMavung.setTypeface(Utils.getTypefaceRegular(DangkiNhapPhoneActivity.this));
		titleNhapSodt.setTypeface(Utils.getTypefaceRegular(DangkiNhapPhoneActivity.this));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_function1:
			showDialogThongbao();
			break;
		case R.id.btn_tieptuc:
			if (edtPhone.getText().toString().equals("")){
				Toast.makeText(DangkiNhapPhoneActivity.this, "Số điện thoại không được để trống.", Toast.LENGTH_SHORT).show();
			} else {
				KeyboardHelper.hideSoftKeyboard(DangkiNhapPhoneActivity.this);
				showDialogThongbao();
			}
			break;
		case R.id.btn_edit:
			dConfirm.dismiss();
			break;
			
		case R.id.btn_ok:
			dConfirm.dismiss();
			closeActivity();
			break;
			
		default:
			break;
		}
	}
	
	void showDialogThongbao(){
		dConfirm = new Dialog(this);
		dConfirm.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dConfirm.setContentView(R.layout.dialog_confirm_send_phone);
		
		TextView txtTitle = (TextView) dConfirm.findViewById(R.id.title_confirm);
		TextView txtThongbao = (TextView) dConfirm.findViewById(R.id.str_send_phone);
		
		Button btnChinhsua = (Button) dConfirm.findViewById(R.id.btn_edit);
		Button btnOk = (Button) dConfirm.findViewById(R.id.btn_ok);
		dConfirm.show();
		dConfirm.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dConfirm.setCanceledOnTouchOutside(true);
		dConfirm.getWindow().setGravity(Gravity.CENTER);
		dConfirm.show();
		
		txtTitle.setTypeface(Utils.getTypefaceBold(DangkiNhapPhoneActivity.this));
		txtThongbao.setTypeface(Utils.getTypefaceMedium(DangkiNhapPhoneActivity.this));
		btnChinhsua.setTypeface(Utils.getTypefaceRegular(DangkiNhapPhoneActivity.this));
		btnOk.setTypeface(Utils.getTypefaceRegular(DangkiNhapPhoneActivity.this));
		// setonclick
		btnChinhsua.setOnClickListener(this);
		btnOk.setOnClickListener(this);
	}
	
	void sendPhone(){
		btnTieptuc.setText("Chờ xác thực...");
		edtPhone.setEnabled(false);
		
		/*HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				super.onFailure(error);
				btnTieptuc.setText("Tiếp tục");
				edtPhone.setEnabled(true);
				error.printStackTrace();
				Toast.makeText(DangkiNhapPhoneActivity.this, "Số điện thoại đã tồn tại trên hệ thống.", Toast.LENGTH_SHORT).show();
			}
			

			@Override
			@Deprecated
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				edtPhone.setEnabled(true);
				Toast.makeText(DangkiNhapPhoneActivity.this, "Số điện thoại không đúng.", Toast.LENGTH_SHORT).show();
			}


			@Override
			public void onSuccess(JSONObject re) {
				btnTieptuc.setText("Tiếp tục");
				Log.i("NHAPPHONE", "TRA VE: "+re.toString());
				
				edtPhone.setEnabled(true);
				
				try {
					accID = String.valueOf(re.getInt("acc_id"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				gotoNhapMaCode();
				
			}
		});*/
		
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, JSONObject re) {
				if (statusCode == 200){
					btnTieptuc.setText("Tiếp tục");
					Log.i("NHAPPHONE", "TRA VE: "+re.toString());
					
					edtPhone.setEnabled(true);
					
					try {
						accID = String.valueOf(re.getInt("acc_id"));
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					gotoNhapMaCode();
				}
			}

			@Override
			public void onFailure(int statusCode, Throwable error) {
				if (statusCode == 400){
					edtPhone.setEnabled(true);
					btnTieptuc.setText("Tiếp tục");
					Toast.makeText(DangkiNhapPhoneActivity.this, "Số điện thoại không đúng.", Toast.LENGTH_SHORT).show();
				} else if (statusCode == 403) {
					btnTieptuc.setText("Tiếp tục");
					edtPhone.setEnabled(true);
					error.printStackTrace();
					Toast.makeText(DangkiNhapPhoneActivity.this, "Số điện thoại đã tồn tại trên hệ thống.", Toast.LENGTH_SHORT).show();
				}
				
			}

		});
		
		httpClientHelper.sendPhone("84"+edtPhone.getText().toString());
		
	}
	
	void gotoNhapMaCode(){
		Intent t = new Intent(DangkiNhapPhoneActivity.this, DangkiNhapCodeActivity.class);
		t.putExtra("acc_id", accID);
		startActivity(t);
		overridePendingTransition(R.anim.trans_left_in, R.anim.over_main_left);
	}
	
	
	
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeCallbacks(r);
		mHandler.removeCallbacksAndMessages(null);
		KeyboardHelper.hideSoftKeyboard(DangkiNhapPhoneActivity.this);
	}

	@Override
	public void onBackPressed() {
		closeActivity();
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.over_main_right, R.anim.trans_right_out);
	}
	
	
}
